//
//  DLShareSDKManager.m
//  delongzhitou
//
//  Created by Mr.Wang on 2019/1/3.
//  Copyright © 2019 delongzhitou. All rights reserved.
//

#import "DLShareSDKManager.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>

@implementation DLShareSDKManager

+(DLShareSDKManager *)share {
    static DLShareSDKManager *shareManager = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        shareManager = [[self alloc] init];
    });
    return shareManager;
}

-(void)shareWithTag:(NSInteger)tag andUrl:(NSString *)url andImage:(NSString *)image andTitle:(NSString *)title andContent:(NSString *)content andShareFinallTipBlock:(void (^)(NSString *tip))shareFinallBlock {
    
    SSDKPlatformType platformType;
    switch (tag) {
        case 0:
            // QQ
            platformType = SSDKPlatformSubTypeQQFriend;
            break;
        case 1:
            // QQ 空间
            platformType = SSDKPlatformSubTypeQZone;
            break;
        case 2:
            // 微信好友
            platformType = SSDKPlatformSubTypeWechatSession;
            break;
        case 3:
            // 微信朋友圈
            platformType = SSDKPlatformSubTypeWechatTimeline;
            break;
        default:
            platformType = 0;
            break;
    }
    
    NSArray * images = @[image];
    if (images) {
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:content
                                         images:images
                                            url:[NSURL URLWithString:url]
                                          title:title
                                           type:SSDKContentTypeImage];
//        //有的平台要客户端分享需要加此方法，例如微博
//        content = [NSString stringWithFormat:@"%@%@",content,url];
//        [shareParams SSDKSetupShareParamsByText:content
//                                         images:images
//                                            url:[NSURL URLWithString:url]
//                                          title:title
//                                           type:SSDKContentTypeAuto];
        
        
        [ShareSDK share:platformType parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
            NSString *tipStr;
            if (error) {
                tipStr = @"取消分享";
                if (shareFinallBlock) {
                    shareFinallBlock(tipStr);
                }
                NSLog(@"分享失败%@", error);
                
//                200104 qq失败,
                
                return;
            }
            switch (state) {
                case SSDKResponseStateSuccess:
                {
                    NSLog(@"plate===%ld",platformType);
                    tipStr = @"分享成功";
                    if (shareFinallBlock) {
                        shareFinallBlock(tipStr);
                    }
                    NSLog(@"分享成功");
                }
                    break;
                case SSDKResponseStateFail:
                {
                    tipStr = @"分享失败";
                    if (shareFinallBlock) {
                        shareFinallBlock(tipStr);
                    }
                    NSLog(@"分享失败");
                }
                    break;
                case SSDKResponseStateCancel:
                {
                    tipStr = @"取消分享";
                    if (shareFinallBlock) {
                        shareFinallBlock(tipStr);
                    }
                    NSLog(@"取消分享");
                }
                    break;
                default:
                    break;
            }
        }];
    }
}

@end
